package cn.xeblog.plugin.action.handler.command;

import cn.hutool.core.util.NumberUtil;
import cn.xeblog.plugin.action.ConsoleAction;
import cn.xeblog.plugin.action.GameAction;
import cn.xeblog.plugin.action.ToolAction;
import cn.xeblog.plugin.annotation.DoCommand;
import cn.xeblog.plugin.cache.DataCache;
import cn.xeblog.plugin.enums.Command;
import cn.xeblog.plugin.tools.Tools;

/**
 * 打开工具命令
 *
 * @author anlingyi
 * @date 2022/8/5 5:34 上午
 */
@DoCommand(Command.OPEN)
public class OpenCommandHandler extends AbstractCommandHandler {

  @Override
  protected void process(String[] args) {
    int len = args.length;
    if (len < 1) {
      // 当命令没有参数时，将工具以列表的形式展示
      StringBuilder sb = new StringBuilder();
      Tools[] tools = Tools.values();
      for (int i = 0; i < tools.length; i++) {
        Tools tool = tools[i];
        sb.append(i).append(".").append(tool.getName()).append("\n");
      }

      ConsoleAction.showSimpleMsg(sb.toString());
      return;
    }

    // 参数必须为数字
    if (!NumberUtil.isNumber(args[0])) {
      ConsoleAction.showSimpleMsg("[" + args[0] + "]" + "不正确的工具编号！");
      return;
    }

    // 工具枚举对象
    Tools tools = Tools.getTool(Integer.parseInt(args[0]));
    if (tools == null) {
      ConsoleAction.showSimpleMsg("没有找到该工具！");
      return;
    }

    // 必须登录但用户不在线
    if (tools.isRequiredLogin() && !DataCache.isOnline) {
      ConsoleAction.showLoginMsg();
      return;
    }

    // 用户还在游戏中
    if (GameAction.playing()) {
      ConsoleAction.showSimpleMsg("请先退出游戏！");
      return;
    }

    // 当前已是所属状态
    if (ToolAction.getTools() == tools) {
      ConsoleAction.showSimpleMsg(tools.getName() + "当前已是开启状态!");
      return;
    }

    ToolAction.create(tools);

    // 提示信息
    ConsoleAction.showSimpleMsg(tools.getName() + "已开启！");
  }

  @Override
  protected boolean check(String[] args) {
    return true;
  }

}
